<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="新闻标题" prop="newTitle">
            <el-input v-model="form.newTitle" placeholder="请输入新闻标题" show-word-limit maxlength="50"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新闻类型" prop="newType">
            <el-select v-model="form.newType" placeholder="请选择新闻类型">
              <el-option
                v-for="dict in dict.type.sys_notice_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="新闻封面" prop="newsImage">
              <image-upload v-model="form.newsImage"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容">
            <editor v-model="form.newContent" :min-height="192"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align:center">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <!-- 添加或修改公告对话框 -->
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import {addNews, updateNews} from "@/api/system/news";

export default {
  name: "publishNews",
  dicts: ['sys_notice_status', 'sys_notice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        newTitle: [
          { required: true, message: "新闻标题不能为空", trigger: "blur" }
        ],
        newType: [
          { required: true, message: "新闻类型不能为空", trigger: "change" }
        ],
        newsImage: [
          { required: true, message: "新闻封面不能为空", trigger: "change" }
        ],
        status:[
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  // created() {
  //   this.getList();
  // },
  methods: {
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        newId: null,
        newTitle: null,
        newType: null,
        newContent: null,
        status: '',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addNews(this.form).then(response => {
            this.$modal.msgSuccess("保存成功");
            // this.$router.push(
            //   {
            //     path:'../newsManagement/index'
            //   }
            // )
          });
        }
      });
    },
  }
};
</script>
<style scoped lang="scss">

</style>
